package cn.ctodb.notebook.wxapp.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.notebook.wxapp.service.PicService;
import cn.ctodb.notebook.wxapp.web.rest.errors.BadRequestAlertException;
import cn.ctodb.notebook.wxapp.web.rest.util.HeaderUtil;
import cn.ctodb.notebook.wxapp.service.dto.PicDTO;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Pic.
 */
@RestController
@RequestMapping("/api")
public class PicResource {

    private final Logger log = LoggerFactory.getLogger(PicResource.class);

    private static final String ENTITY_NAME = "pic";

    private final PicService picService;

    public PicResource(PicService picService) {
        this.picService = picService;
    }

    /**
     * POST  /pics : Create a new pic.
     *
     * @param picDTO the picDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new picDTO, or with status 400 (Bad Request) if the pic has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/pics")
    @Timed
    public ResponseEntity<PicDTO> createPic(@RequestBody PicDTO picDTO) throws URISyntaxException {
        log.debug("REST request to save Pic : {}", picDTO);
        if (picDTO.getId() != null) {
            throw new BadRequestAlertException("A new pic cannot already have an ID", ENTITY_NAME, "idexists");
        }
        PicDTO result = picService.save(picDTO);
        return ResponseEntity.created(new URI("/api/pics/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /pics : Updates an existing pic.
     *
     * @param picDTO the picDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated picDTO,
     * or with status 400 (Bad Request) if the picDTO is not valid,
     * or with status 500 (Internal Server Error) if the picDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/pics")
    @Timed
    public ResponseEntity<PicDTO> updatePic(@RequestBody PicDTO picDTO) throws URISyntaxException {
        log.debug("REST request to update Pic : {}", picDTO);
        if (picDTO.getId() == null) {
            return createPic(picDTO);
        }
        PicDTO result = picService.save(picDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, picDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /pics : get all the pics.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of pics in body
     */
    @GetMapping("/pics")
    @Timed
    public List<PicDTO> getAllPics() {
        log.debug("REST request to get all Pics");
        return picService.findAll();
        }

    /**
     * GET  /pics/:id : get the "id" pic.
     *
     * @param id the id of the picDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the picDTO, or with status 404 (Not Found)
     */
    @GetMapping("/pics/{id}")
    @Timed
    public ResponseEntity<PicDTO> getPic(@PathVariable Long id) {
        log.debug("REST request to get Pic : {}", id);
        PicDTO picDTO = picService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(picDTO));
    }

    /**
     * DELETE  /pics/:id : delete the "id" pic.
     *
     * @param id the id of the picDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/pics/{id}")
    @Timed
    public ResponseEntity<Void> deletePic(@PathVariable Long id) {
        log.debug("REST request to delete Pic : {}", id);
        picService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
